package com.kungfuhacking.wristbandpro.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.kungfuhacking.wristbandpro.R;
import com.kungfuhacking.wristbandpro.base.view.BaseActivity;
import com.kungfuhacking.wristbandpro.base.view.HomeActivity;
import com.kungfuhacking.wristbandpro.custom.TitleBarView;
import com.kungfuhacking.wristbandpro.utils.Constant;
import com.kungfuhacking.wristbandpro.utils.LogUtil;
import com.tencent.mm.sdk.constants.ConstantsAPI;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

/**
 * 微信支付回调页面
 * Created by Aaron on 16/9/13.
 */
public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler {

    private static final String TAG = "WXPayEntryActivity";

    private IWXAPI api;
    //    private android.widget.TextView tvinfo;
    private TextView tv_status;
    private ImageView iv_status;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_result);

        initView();

        api = WXAPIFactory.createWXAPI(this, Constant.APP_ID_WEIXIN);
        api.handleIntent(getIntent(), this);

    }

    private void initView() {

        TitleBarView tbv = (TitleBarView) findViewById(R.id.tbv);
        tbv.setOnLeftClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goback();
            }
        });

        tv_status = (TextView) findViewById(R.id.tv_status);
        iv_status = (ImageView) findViewById(R.id.iv_status);

    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        goback();
    }

    /**
     * 返回到主页面
     */
    private void goback() {
        nextActivityThenKill(HomeActivity.class);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }


    @Override
    public void onResp(BaseResp resp) {

        LogUtil.d(TAG, "errCode=:" + resp.errCode + ", resp type类型:" + resp.getType() + ", transaction : " + resp.transaction);

        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {

            switch (resp.errCode) {

                /*
                0	成功	展示成功页面
-1	错误	可能的原因：签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。
-2	用户取消	无需处理。发生场景：用户不支付了，点击取消，返回APP。
                 */
// E/WXPayEntryActivity: errCode=-2
//            09-19 15:51:19.779 32585-32585/? E/tag:  resp type类型:5
//                case -2:
//
//                    //用户取消支付
//                    showToast("支付成功");
//
//                    break;

                case 0:
                    iv_status.setImageResource(R.mipmap.ic_pay_success);
                    tv_status.setText("支付成功");
                    break;

                default:
                    iv_status.setImageResource(R.mipmap.ic_slide_delete);
                    tv_status.setText("支付失败");
                    break;

            }

        }

    }

    @Override
    public void onReq(BaseReq baseReq) {

    }
}
